home *** CD-ROM | disk | FTP | other *** search
/ CICA 1993 April / CICA MS Windows - April 1993.iso / unzipped / programr / tp / arrays / arrays.pas
Pascal/Delphi Source File  |  1992-02-07  |  6KB  |  217 lines

  1. Unit Arrays;
  2.  
  3. {Author ROBERT WARREN CIS ID 70303,537}
  4.  
  5. {
  6.     Unit provides support for arrays and huge (>64K) arrays of data.
  7.     Each item size stored in the array must be a multiple of 2 (2,4,8,16,32..)
  8.     in order to work using the huge arrays and matrices. This is needed so
  9.     that you don't straddle the segment boundaries.
  10.  
  11.     To use these objects merely create the object using the appropriate
  12.     constructor. The object will be created with it's data initialized to
  13.     zeros (GMEM_ZEROINIT). to access an element of the object use the
  14.     AT() method. This will return you a pointer to the element you specified.
  15.     for huge objects it will do the segment math correctly. Then you can
  16.     dereference the pointer and work with the data.
  17.  
  18.     This unit merely simplifies the use of huge type arrays and matrices.
  19.     It does not do much more.
  20.  
  21. }
  22.  
  23. interface
  24.  
  25. uses WObjects,WinTypes,WinProcs;
  26.  
  27. type
  28.  
  29. PArray = ^TArray;
  30. TArray = object(TObject)
  31.     Handle: THandle;
  32.     ItemSize: Word;
  33.     Limit: LongInt;
  34.     Address: Pointer;
  35.     constructor Init(aItemSize: Word; aLimit: LongInt);
  36.     destructor done; virtual;
  37.     function At(index: LongInt): Pointer; virtual;
  38.  end;
  39.  
  40. PMatrix = ^TMatrix;
  41. TMatrix = object(TObject)
  42.     Handle: THandle;
  43.     ItemSize: Word;
  44.     Rows,Cols: LongInt;
  45.     Address: Pointer;
  46.     constructor Init(aItemSize: Word; aRows,aCols: LongInt);
  47.     destructor done; virtual;
  48.     function At(aRow,aCol: LongInt): Pointer; virtual;
  49.  end;
  50.  
  51. PHugeMatrix = ^THugeMatrix;
  52. THugeMatrix = object(TMatrix)
  53.     SegIncr : Word;
  54.     constructor Init(aItemSize: Word; aRows,aCols: LongInt);
  55.     function At(aRow,aCol: LongInt): Pointer; virtual;
  56.  end;
  57.  
  58. PHugeArray = ^THugeArray;
  59. THugeArray = object(TArray)
  60.     SegIncr: Word;
  61.     constructor Init(aItemSize: Word; aLimit: LongInt);
  62.     function At(index: LongInt): Pointer; virtual;
  63. end;
  64.  
  65. function NewArray(aItemSize: Word; aLimit: LongInt): PArray;
  66. function NewMatrix(aItemSize: Word; aRows,aCols: LongInt): PMatrix;
  67.  
  68. implementation
  69.  
  70. {
  71.  returns a pointer to an Array if small enough otherwise a HugeArray
  72. }
  73. function NewArray(aItemSize: Word; aLimit: LongInt): PArray;
  74. var
  75.  TempArrayPtr: PArray;
  76. begin
  77.  TempArrayPtr:=New(PArray,Init(aItemSize,aLimit));
  78.  if TempArrayPtr = nil then
  79.     TempArrayPtr:=New(PHugeArray,Init(aItemSize,aLimit));
  80.  NewArray:=TempArrayPtr;
  81. end;
  82.  
  83. {
  84.  returns a pointer to an Matrix if small enough otherwise a HugeMatrix
  85. }
  86. function NewMatrix(aItemSize: Word; aRows,aCols: LongInt): PMatrix;
  87. var
  88.  TempMatrixPtr: PMatrix;
  89. begin
  90.  TempMatrixPtr:=New(PMatrix,Init(aItemSize,aRows,aCols));
  91.  if TempMatrixPtr = nil then
  92.     TempMatrixPtr:=New(PHugeMatrix,Init(aItemSize,aRows,aCols));
  93.  NewMatrix:=TempMatrixPtr;
  94. end;
  95.  
  96. procedure AHIncr; far; external 'KERNEL' index 114;
  97.  
  98. { ----------------------------------------------
  99.                 TMatrix
  100.     ---------------------------------------------- }
  101.  
  102. constructor TMatrix.Init(aItemSize: Word; aRows,aCols: LongInt);
  103. var
  104.  InitSize: LongInt;
  105. begin
  106.     TObject.Init;
  107.     Rows:=aRows;
  108.     Cols:=aCols;
  109.     ItemSize:=aItemSize;
  110.     InitSize:=LongInt(ItemSize * Rows * Cols);
  111.     if InitSize > $FFFF then fail;
  112.     Handle:=GlobalAlloc(GMEM_MOVEABLE or GMEM_ZEROINIT,ItemSize * Rows * Cols);
  113.     if handle = 0 then fail;
  114.     Address:=GlobalLock(Handle);
  115. end;
  116.  
  117. destructor TMatrix.done;
  118. begin
  119.  GlobalUnlock(Handle);
  120.  GlobalFree(Handle);
  121. end;
  122.  
  123. function TMatrix.At(aRow,aCol: LongInt): Pointer;
  124. var
  125.  pos: Word;
  126. begin
  127.  pos:=(aRow * Cols * ItemSize) + (ACol * ItemSize);
  128.  At:=Pointer(MakeLong(pos,HiWord(LongInt(Address))));
  129. end;
  130.  
  131. { ----------------------------------------------
  132.                 THugeMatrix
  133.     ---------------------------------------------- }
  134.  
  135. constructor THugeMatrix.Init(aItemSize: Word; aRows,aCols: LongInt);
  136. begin
  137.     TObject.Init;
  138.     Rows:=aRows;
  139.     Cols:=aCols;
  140.     ItemSize:=aItemSize;
  141.  
  142.     Handle:=GlobalAlloc(GMEM_MOVEABLE or GMEM_ZEROINIT,LongInt(ItemSize * Rows * Cols));
  143.     if handle = 0 then fail;
  144.     Address:=GlobalLock(Handle);
  145.     SegIncr:=Ofs(AHIncr);
  146. end;
  147.  
  148. function THugeMatrix.At(aRow,aCol: LongInt): Pointer;
  149. var
  150.  Segs,Offs: Word;
  151.  Pos: LongInt;
  152. begin
  153.     pos:=(aRow * Cols * ItemSize) + (ACol * ItemSize);
  154.     Segs:=Pos div $FFFF;
  155.     Offs:=Pos mod $FFFF;
  156.     At:=Pointer(MakeLong(Offs,((Segs*SegIncr)+(HiWord(LongInt(Address))))));
  157. end;
  158.  
  159.  
  160. { ----------------------------------------------
  161.                 TArray
  162.     ---------------------------------------------- }
  163.  
  164. constructor TArray.Init(aItemSize: Word; aLimit: LongInt);
  165. var
  166.  InitSize: LongInt;
  167. begin
  168.     TObject.Init;
  169.     ItemSize:=aItemSize;
  170.     Limit:=aLimit;
  171.     InitSize:=ItemSize * Limit;
  172.     if InitSize > $FFFF then fail;
  173.     Handle:=GlobalAlloc(GMEM_MOVEABLE or GMEM_ZEROINIT,InitSize);
  174.     if handle = 0 then fail;
  175.     Address:=GlobalLock(Handle);
  176. end;
  177.  
  178. destructor TArray.Done;
  179. begin
  180.  TObject.Done;
  181.  GlobalUnlock(Handle);
  182.  GlobalFree(Handle);
  183. end;
  184.  
  185. function TArray.At(index: LongInt): Pointer;
  186. begin
  187.     At:=Pointer(LongInt(ItemSize * index) + LongInt(Address));
  188. end;
  189.  
  190. { ----------------------------------------------
  191.                 THugeArray
  192.     ---------------------------------------------- }
  193.  
  194. constructor THugeArray.Init(aItemSize: Word; aLimit: LongInt);
  195. begin
  196.     TObject.Init;
  197.     ItemSize:=aItemSize;
  198.     Limit:=aLimit;
  199.     Handle:=GlobalAlloc(GMEM_MOVEABLE or GMEM_ZEROINIT,ItemSize * Limit);
  200.     if handle = 0 then fail;
  201.     Address:=GlobalLock(Handle);
  202.     SegIncr:=Ofs(AHIncr);
  203. end;
  204.  
  205. function THugeArray.At(index: LongInt): Pointer;
  206. var
  207.  Segs,Offs: Word;
  208.  Pos: LongInt;
  209. begin
  210.     Pos:=Index * ItemSize;
  211.     Segs:=Pos div $FFFF;
  212.     Offs:=Pos mod $FFFF;
  213.     At:=Pointer(MakeLong(Offs,((Segs*SegIncr)+(HiWord(LongInt(Address))))));
  214. end;
  215.  
  216. begin
  217. end.